package cn.matezk.cloud.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.matezk.cloud.dao.DeptDao;
import cn.matezk.cloud.model.Dept;
import cn.matezk.cloud.service.IDeptService;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class DeptService extends TencentService implements IDeptService {
    @Autowired
    private DeptDao deptDao;

    @Override
    public void load(String token, Long useId) throws TencentCloudSDKException {
        JSONObject params = JSONUtil.createObj()
                .set("AccessToken", token)
                .set("Action", "AppGetFamilyList");
        JSONObject response = request(params);
        params.set("Action", "AppGetRoomList");
        JSONArray families = response.getJSONArray("FamilyList");
        for (int i = 0; i < families.size(); i ++) {
            JSONObject family = families.getJSONObject(i);
            String familyId = family.getStr("FamilyId");
            Dept familyDept = deptDao.selectByExtId(family.getStr("FamilyId"));
            if (ObjectUtil.isNull(familyDept)) {
                familyDept = new Dept();
                Long id = IdUtil.createSnowflake(0, 0).nextId();
                familyDept.setId(id);
                familyDept.setExtId(familyId);
                familyDept.setParentId(0L);
                familyDept.setAncestors("0");
                familyDept.setName(family.getStr("FamilyName"));
                familyDept.setCreateUser(useId);
                familyDept.setCreateTime(new Date());
                deptDao.insertSelective(familyDept);
            } else {
                familyDept.setName(family.getStr("FamilyName"));
                deptDao.updateByPrimaryKeySelective(familyDept);
            }
            // 查询房间
            params.set("FamilyId", familyId);
            response = request(params);
            JSONArray rooms = response.getJSONArray("RoomList");
            for (int j = 0; j < rooms.size(); j ++) {
                JSONObject room = rooms.getJSONObject(j);
                String roomId = room.getStr("RoomId");
                Dept roomDept = deptDao.selectByExtId(roomId);
                if (ObjectUtil.isNull(roomDept)) {
                    roomDept = new Dept();
                    Long id = IdUtil.createSnowflake(0, 0).nextId();
                    roomDept.setId(id);
                    roomDept.setExtId(roomId);
                    roomDept.setParentId(familyDept.getId());
                    roomDept.setAncestors(String.format("0,%d", familyDept.getId()));
                    roomDept.setName(room.getStr("RoomName"));
                    roomDept.setCreateUser(useId);
                    roomDept.setCreateTime(new Date());
                    deptDao.insertSelective(roomDept);
                } else {
                    roomDept.setName(room.getStr("RoomName"));
                    deptDao.updateByPrimaryKeySelective(roomDept);
                }
            }
        }
    }

    @Override
    public JSONObject appCreateFamily(JSONObject params) throws TencentCloudSDKException {
        Long userId = params.getJSONObject("ext").getLong("userId");
        params.remove("ext");
        JSONObject response = request(params);
        String familyId = response.getJSONObject("Data").getStr("FamilyId");
        Dept dept = new Dept();
        Long id = IdUtil.createSnowflake(0, 0).nextId();
        dept.setId(id);
        dept.setExtId(familyId);
        dept.setParentId(0L);
        dept.setAncestors("0");
        dept.setName(params.getStr("Name"));
        dept.setCreateUser(userId);
        dept.setCreateTime(new Date());
        deptDao.insertSelective(dept);
        return response;
    }

    @Override
    public JSONObject appModifyFamily(JSONObject params) throws TencentCloudSDKException {
        Long userId = params.getJSONObject("ext").getLong("userId");
        params.remove("ext");
        JSONObject response = request(params);
        String familyId = params.getStr("FamilyId");
        Dept dept = deptDao.selectByExtId(familyId);
        if (ObjectUtil.isNull(dept)) {
            dept = new Dept();
            Long id = IdUtil.createSnowflake(0, 0).nextId();
            dept.setId(id);
            dept.setExtId(familyId);
            dept.setParentId(0L);
            dept.setAncestors("0");
            dept.setName(params.getStr("Name"));
            dept.setCreateUser(userId);
            dept.setCreateTime(new Date());
            deptDao.insertSelective(dept);
        } else {
            dept.setName(params.getStr("Name"));
            dept.setUpdateUser(userId);
            dept.setUpdateTime(new Date());
            deptDao.updateByPrimaryKeySelective(dept);
        }
        return response;
    }

    @Override
    public JSONObject appDeleteFamily(JSONObject params) throws TencentCloudSDKException {
        JSONObject response = request(params);
        deptDao.deleteByExtId(params.getStr("FamilyId"));
        return response;
    }

    @Override
    public JSONObject appCreateRoom(JSONObject params) throws TencentCloudSDKException {
        Long userId = params.getJSONObject("ext").getLong("userId");
        params.remove("ext");
        JSONObject response = request(params);
        String roomId = response.getJSONObject("Data").getStr("RoomId");
        String familyId = params.getStr("FamilyId");
        Dept family = deptDao.selectByExtId(familyId);
        if (ObjectUtil.isNull(family)) {
            // 查询家庭信息
            JSONObject familyParams = JSONUtil.createObj()
                    .set("AccessToken", params.getStr("AccessToken"))
                    .set("Action", "AppDescribeFamily").set("FamilyId", familyId);
            JSONObject familyResponse = request(familyParams);
            family = new Dept();
            Long id = IdUtil.createSnowflake(0, 0).nextId();
            family.setId(id);
            family.setExtId(familyId);
            family.setParentId(0L);
            family.setAncestors("0");
            family.setName(familyResponse.getJSONObject("Data").getStr("FamilyName"));
            family.setCreateUser(userId);
            family.setCreateTime(new Date());
            deptDao.insertSelective(family);
        }
        Dept dept = new Dept();
        Long id = IdUtil.createSnowflake(0, 0).nextId();
        dept.setId(id);
        dept.setExtId(roomId);
        dept.setParentId(family.getId());
        dept.setAncestors(String.format("0,%d", family.getId()));
        dept.setName(params.getStr("Name"));
        dept.setCreateUser(userId);
        dept.setCreateTime(new Date());
        deptDao.insertSelective(dept);
        return response;
    }

    @Override
    public JSONObject appModifyRoom(JSONObject params) throws TencentCloudSDKException {
        Long userId = params.getJSONObject("ext").getLong("userId");
        params.remove("ext");
        JSONObject response = request(params);
        String familyId = params.getStr("FamilyId");
        Dept family = deptDao.selectByExtId(familyId);
        if (ObjectUtil.isNull(family)) {
            // 查询家庭信息
            JSONObject familyParams = JSONUtil.createObj()
                    .set("AccessToken", params.getStr("AccessToken"))
                    .set("Action", "AppDescribeFamily").set("FamilyId", familyId);
            JSONObject familyResponse = request(familyParams);
            family = new Dept();
            Long id = IdUtil.createSnowflake(0, 0).nextId();
            family.setId(id);
            family.setExtId(familyId);
            family.setParentId(0L);
            family.setAncestors("0");
            family.setName(familyResponse.getJSONObject("Data").getStr("FamilyName"));
            family.setCreateUser(userId);
            family.setCreateTime(new Date());
            deptDao.insertSelective(family);
        }
        String roomId = params.getStr("RoomId");
        Dept room = deptDao.selectByExtId(roomId);
        if (ObjectUtil.isNull(room)) {
            room = new Dept();
            Long id = IdUtil.createSnowflake(0, 0).nextId();
            room.setId(id);
            room.setExtId(roomId);
            room.setParentId(family.getId());
            room.setAncestors(String.format("0,%d", family.getId()));
            room.setName(params.getStr("Name"));
            room.setCreateUser(userId);
            room.setCreateTime(new Date());
            deptDao.insertSelective(room);
        } else {
            room.setName(params.getStr("Name"));
            deptDao.updateByPrimaryKeySelective(room);
        }
        return response;
    }

    @Override
    public JSONObject appDeleteRoom(JSONObject params) throws TencentCloudSDKException {
        JSONObject response = request(params);
        deptDao.deleteByExtId(params.getStr("RoomId"));
        return response;
    }
}
